/**日志管理 js
 * @author wangze
 * @date 2017-12-19 9:46.
 **/

(function () {
    var _bt = window.BT,
        _util = _bt.util,
        _debug = _util.debug,
        _dialog = _util.dialog,
        _config = _util.config,
        _enum = _config.enumValues,
        _url = _config.url,
        _logUrl = _url.log;


    var gridOpt = {
        url: _logUrl.list,
        // selection: false,
        // multiSelect: false,
        formatters: {
            "logFormat": function (column, row) {
                return '<button class="btn btn-xs  command-view" data-row-id="{0}" data-row-user-name="{1}"  data-row-count="{2}">查看</button>'.format(row.id, row.name, row.logincount);
            }
        }
    };
    var page = {
        grid: null,
        init: function () {
            page.grid = new _bt.BGrid(gridOpt);
            page.bind();
        },
        selectIds:[]
    };

    /**
     *用户历史记录
     */
    page.history = {
        dialog: '',
        grid: null,
        show: function (title, id, userName, count) {
            var m = _dialog.edit({
                message: $('#history-template').html(),
                title: title
            }, {
                init: function () {
                    page.history.grid = new _bt.BGrid({
                        id: '#grid-history',
                        url: encodeURI(_logUrl.history + '?userName=' + userName)
                    });
                    $('#history-user-name').text(userName);
                    $('#history-count').text(count);
                    $('.modal-footer').css('text-align', 'center').find('button:eq(0)').text('导出');
                },
                success: function () {
                    var ids = page.history.grid.getSelections();
                    if (parseInt($('#history-count').text(), 10) <= 0) {
                        _dialog.warn('当前没有记录');
                        return;
                    }

                    if (ids.length === 0) {
                        _dialog.warn('请选择一条数据');
                        return;
                    }
                    window.location.href = encodeURI(_logUrl.downloadUser + '?ids=' + ids.join(',') + _util.getApiKeyUrl());
                }
            });
            page.history.dialog = m;
            return m;
        },
        hide: function () {
            page.history.dialog && page.history.dialog.modal('hide');
        }
    };


    /**
     * 绑定事件
     */
    page.bind = function () {

        //点击分页 数据加载前，获取当前选中的数据列
        page.grid.on('load',function (e) {
            _debug.info("load:"+page.selectIds);
        });

        page.grid.on('selected',function (e,rows) {
            var newRows=$.map(rows,function (n,i) {
                return n.id;
            });
            $.each(newRows,function (i,item) {
                if($.inArray(item,page.selectIds)===-1){
                    page.selectIds.push(item);
                }
            });
        });

        page.grid.on('deselected',function (e,rows) {
            var newRows=$.map(rows, function (n,i) {
                return n.id;
            });
            var indexs=[];
            $.each(page.selectIds,function (i,item) {
                var index=$.inArray(item,newRows);
                if(index!==-1){
                  indexs.push(index);
                }
            });
            $.each(indexs,function (i,item) {
                page.selectIds.splice(item,1);
            });
        });

        page.grid.on('loaded', function (e) {
            $('.command-view').on('click', function () {
                var id = $(this).attr('data-row-id');
                var userName = $(this).attr('data-row-user-name');
                var count = $(this).attr('data-row-count');
                page.history.show('系统日志', id, userName, count);
                return false;
            });
            //选中当前页面原先选中的
            //因为这个控件，选中的时候，会删除arry的数据 所以用concat返回副本
            page.grid.select(page.selectIds.concat([]));
        });
        $('#btn-export').on('click', function () {
            var ids = page.selectIds;
            if (ids.length === 0) {
                _dialog.warn('请选择一条数据');
                return;
            }
            window.location.href = encodeURI(_logUrl.downloadAll + '?ids=' + ids.join(',') + _util.getApiKeyUrl());
        });
    };

    $(function () {
        page.init();
    });

})();